package com.yangdi.demo;


import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements JoystickView.JoystickListener {
    private TextView directionText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        directionText = findViewById(R.id.directionText);
        JoystickView joystick = findViewById(R.id.joystick);
        joystick.setJoystickListener(this);
    }

    @Override
    public void onJoystickMoved(float x, float y) {
        String direction = "";
        if (y < -0.5) {
            direction = "上";
        } else if (y > 0.5) {
            direction = "下";
        }
        if (x < -0.5) {
            direction += "左";
        } else if (x > 0.5) {
            direction += "右";
        }
        if (direction.isEmpty()) {
            direction = "中心";
        }
        directionText.setText("方向: " + direction);
    }
}